//
//  ViewController.m
//  SegmentDemo
//
//  Created by 王杰 on 2019/3/18.
//  Copyright © 2019 王杰. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic ,strong) UIViewController * vc1;
@property (nonatomic ,strong) UIViewController * vc2;
@end

@implementation ViewController

- (UIViewController *)vc1{
    if (!_vc1) {
        _vc1 = [[UIViewController alloc] init];
        _vc1.view.backgroundColor = UIColor.redColor;
    }
    return _vc1;
}
- (UIViewController *)vc2{
    if (!_vc2) {
        _vc2 = [[UIViewController alloc] init];
        _vc2.view.backgroundColor = UIColor.yellowColor;

    }
    return _vc2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addChildViewController:self.vc1];
    [self.view addSubview:self.vc1.view];
    [self.vc1 didMoveToParentViewController:self];
    [self addChildViewController:self.vc2];
    [self.vc2 didMoveToParentViewController:self];

    UISegmentedControl * controller = [[UISegmentedControl alloc] initWithItems:@[@"选课",@"上课"]];
    controller.tintColor = UIColor.greenColor;
    controller.selectedSegmentIndex = 0;
    [controller addTarget:self action:@selector(segemntClicked:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = controller;
    controller.frame = CGRectMake(0, 0, 110, 27);

    
}


-(void)segemntClicked:(UISegmentedControl *)control{
    switch (control.selectedSegmentIndex) {
        case 0:{
           
            [self transitionFromViewController:self.vc2 toViewController:self.vc1 duration:0.5f options:UIViewAnimationOptionTransitionNone animations:^{
                
            } completion:nil];
        }
            break;
        case 1:{
            
            [self transitionFromViewController:self.vc1 toViewController:self.vc2 duration:0.5 options:UIViewAnimationOptionTransitionNone animations:^{
                
            } completion:nil];
        }
            break;
            
        default:
            break;
    }
}


@end
